// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function ISODateString(d) {
  function pad(n){
      return n<10 ? '0'+n : n;
  }
  return d.getUTCFullYear()+'-'+ pad(d.getUTCMonth()+1)+'-'+ pad(d.getUTCDate())+'T'+ pad(d.getUTCHours())+':'+ pad(d.getUTCMinutes())+':'+ pad(d.getUTCSeconds())+'Z';
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function setMinDate(period) {
	var oneHour = 1000 * 60 * 60; //
	var today = new Date();
	var tz = today.getTimezoneOffset()*60*1000; //get timezoneoffset in ms
	var t = today.valueOf() + tz ;
	switch(period){
		case "1h":
			t = t - oneHour;
			break;
		case "1d":
			t = t - (oneHour * 24);
			break;
		case "1w":
			t = t - (oneHour * 24 * 7);
			break;
		default:
			t = t - (oneHour * 24 * 30);
			break;
	}
	today.setTime(t);
	myMinDate.value = today.toISOString();
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function myInfo(){
	var dat = new Date();
	var str = "";
	var i = 0;
	if (arguments.length > 0) {
		for (i = 0; i < arguments.length; i++) {
			str = str + arguments[i] + ", ";
		}
	}
	Mojo.Log.info("MyInfo", dat.toLocaleTimeString(), str); //JSON.stringify(arguments));
}
function myError(){
	var dat = new Date();
		Mojo.Log.info("MyERROR", dat.toLocaleTimeString(), JSON.stringify(arguments));
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
var sort_by = function(field, reverse, primer){
   reverse = (reverse) ? -1 : 1;
   return function(a,b){
       a = a[field];
       b = b[field];
       if (typeof(primer) != 'undefined'){
           a = primer(a);
           b = primer(b);
       }
       if (a<b) {return reverse * -1;}
       if (a>b) {return reverse * 1;}
       return 0;
   };
};
//Now you can sort by any field at will
// Sort by price high to low
//homes.sort(sort_by('price', true, parseInt));
// Sort by city, case-insensitive, A-Z
//homes.sort(sort_by('city', false, function(a){return a.toUpperCase()}));

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function addCommas(nStr){
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function calcDist(lat1, lon1, lat2, lon2, unit){
	var radlat1 = Math.PI * lat1/180;
	var radlat2 = Math.PI * lat2/180;
	var radlon1 = Math.PI * lon1/180;
	var radlon2 = Math.PI * lon2/180;
	var theta = lon1-lon2;
	var radtheta = Math.PI * theta/180;
	var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
	dist = Math.acos(dist);
	dist = dist * 180/Math.PI;
	dist = dist * 60 * 1.1515;
	if (unit == "K") { dist = dist * 1.609344; }
	if (unit == "N") { dist = dist * 0.8684; }
	// Mojo.Log.info("+++ calcDist(", lat1, lon1, lat2, lon2, ")=", dist);
	return dist;
}

// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
String.prototype.capitalize=function() {
	return this.replace(/\w+/g,
		function(a){return a.charAt(0).toUpperCase()+a.substr(1).toLowerCase();});
};

